|
|
|
|
|
|
|
Because you are drawing the objects in order, with the closest objects drawn last, closer objects obscure objects that are further away. When you are finished, the objects appear on the screen with the hidden lines buried under objects that are closer to the center of projection. |
|
|
|
|
|
|
|
|
This process is similar to the way you could create a painting. You could begin by painting the background areas. You could then add the objects in the middle distance, painting them over the background. Eventually you would add the foreground objects on top of the background and objects in the middle distance. Because the Depth-Sort algorithm is similar to painting in this way, it is sometimes called the painter's algorithm. |
|
|
|
|
|
|
|
|
Determining the distance from an object to the center of projection can be quite time consuming. For each point in each object, you need to perform several multiplications, additions, and even a square root. |
|
|
|
|
|
|
|
|
You can make the calculations simpler if you apply the projection transformation to the objects before you order them. Recall that the perspective transformation described in Chapter 13, Three-Dimensional Transformations, preserves each point's Z coordinate. After the objects have been translated, rotated, and projected, you can use the points' Z coordinates to decide which is farthest from the center of projection. |
|
|
|
|
|
|
|
|
If the objects are convex solids and they are not too close together, the Depth-Sort algorithm combined with backface removal hides all the hidden surfaces correctly. First, perform backface removal so that you can skip examining backfaces in the later steps. Then arrange the convex solids in order from back to front as seen from the center of projection. Finally, draw the solids in their proper order. |
|
|
|
|
|
|
|
|
The following code shows how a program can sort the objects it contains. The Solid3d class has a SetZmax routine that finds and records the largest transformed Z coordinate value of any of the points it contains. The SortSolids subroutine examines the ZMax values to reorder the Solids collection so that those farthest from the center of projection come first. The program then draws them in this order. |
|
|
|
 |
|
|
|
|
' Sort the solids in depth-sort order. Private Sub SortSolids()
Dim solid As Solid3d
Dim ordered_solids As Collection
Dim besti As Integer
Dim bestz As Single
Dim newz As Single
Dim i As Integer
' Compute each solid's Zmax value. For Each solid In Solids
solid. SetZmax
Next solid
' Sort the objects by their Zmax values. Set ordered_solids = New Collection |
|
|
|
|
|